<?php
namespace app\admin\controller;
use app\admin\controller\AdminBaseController;
use think\Cache;
use think\Db;

/**
 * 财务
 * Class System
 */
class FinanceController extends BaseController {
	/**
	 * 会员认证
	 */
	public function member_verif($status='',$mobile='',$user_name=''){
		$where=[];
		if($user_name){
			$where['user_name']=array('like',"%".$user_name."%");
		}
		$this->assign('user_name',$user_name);
		if($mobile){
			$where['reserve_mobile']=array('like',"%".$mobile."%");
		}
		$this->assign('mobile',$mobile);
		if($status){
			$where['status']=$status;
		}
		$this->assign('status',$status);
		$list=db("member_verif")->where($where)->order("add_time desc")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	//会员查看审核
	public function member_verif_sh(){
		$where['a.id']=input("id");
		$field="a.*,b.nick_name";
		$tx=db('member_verif')
					->alias("a")
					->join("member b","a.user_id=b.user_id")
					->where($where)->field($field)->find();
		$this->assign('tx',$tx);
		return $this->fetch();
	}
	public function member_verif_confirm(){
		$up['deal_time']=now_time();
		$up['status']=input("status");
		$res=db('member_verif')->where("id",input("id"))->update($up);
		if($res){
			$this->success('操作成功',url('finance/member_verif'));
		}else{
			$this->success('操作失败！');
		}
	}
	function member_verif_pass(){
		//获取审核信息
		$verif_info = db('member_verif')->where('id',input('id'))->find();
		$this->assign('verif_info',$verif_info);
		
		if(request()->isAjax()){
			$data = input('post.');
			$data['status'] = 3;
			$res1=db('member_verif')->update($data);
			$res2=db('member')->where('user_id',$verif_info['user_id'])->update(['is_verify'=>4]);
			if($res1){
				return json(['code'=>200,'msg'=>'操作成功']);
			}else{
				return json(['code'=>403,'msg'=>'操作失败']);
			}
		}
		return $this->fetch();
	}
	
	
	/**
	 * 商家认证
	 */
	public function agent_verif($status='',$mobile='',$agent_name=''){
		$where=[];
		if($agent_name){
			$where['a.agent_name']=array('like',"%".$agent_name."%");
		}
		$this->assign('agent_name',$agent_name);
		if($mobile){
			$where['reserve_mobile']=array('like',"%".$mobile."%");
		}
		$this->assign('mobile',$mobile);
		if($status){
			$where['a.status']=$status;
		}
		$this->assign('status',$status);
		
		$list=db("agent_verif")
				->alias("a")
				->join("agent b","a.agent_id=b.id",'left')
				->field("a.*,b.mobile,b.agent_name")
				->where($where)
				->order("add_time desc")
				->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	//商家申请认证查看审核
	public function agent_verif_sh(){
		$where['a.id']=input("id");
		$field="a.*,b.agent_name";
		$tx=db('agent_verif')->alias("a")->join("agent b","a.agent_id=b.id")->where($where)->field($field)->find();
		$this->assign('tx',$tx);
		return $this->fetch();
	}
	
	function agent_verif_pass(){
		//获取审核信息
		$verif_info = db('agent_verif')->where('id',input('id'))->find();
		$this->assign('verif_info',$verif_info);
		if(request()->isAjax()){
			$data = input('post.');
			$data['status'] = 3;
			$res1=db('agent_verif')->update($data);
			$res2=db('agent')->where('id',$verif_info['agent_id'])->update(['is_verify'=>4]);
			if($res1){
				return json(['code'=>200,'msg'=>'操作成功']);
			}else{
				return json(['code'=>403,'msg'=>'操作失败']);
			}
		}
		return $this->fetch();
	}
	
	
	function agent_verif_confirm(){
		$up['deal_time']=now_time();
		$up['status']=input("status");
		$res1=db('agent_verif')->where("id",input("id"))->update($up);
		$verif_info = db('agent_verif')->where("id",input("id"))->find();
		$res2=db('agent')->where('id',$verif_info['agent_id'])->update(['is_verify'=>2]);
		if($res1){
			$this->success('操作成功',url('finance/agent_verif'));
		}else{
			$this->success('操作失败！');
		}
	}
	
    //用户提现
	public function member_tx($status='',$mobile='',$user_name=''){
		$where=[];
		if($user_name){
			$where['a.user_name']=array('like',"%".$user_name."%");
		}
		$this->assign('user_name',$user_name);
		if($mobile){
			$where['reserve_mobile']=array('like',"%".$mobile."%");
		}
		$this->assign('mobile',$mobile);
		if($status){
			$where['a.status']=$status;
		}
		$this->assign('status',$status);
		
		$field="a.*,b.nick_name,b.mobile";
		$tx=db('member_tixian')
					->alias("a")
					->join("member b","a.user_id=b.user_id")
					->where($where)->field($field)
					->order("add_time desc")
					->paginate(15);
		$this->assign('list',$tx);
		return $this->fetch();
	}
	//用户申请提现查看审核
	public function member_tx_sh(){
		$where['a.id']=input("id");
		$field="a.*,b.nick_name,b.mobile";
		$tx=db('member_tixian')->alias("a")->join("member b","a.user_id=b.user_id")->where($where)->field($field)->find();
		$this->assign('tx',$tx);
		return $this->fetch();
	}
	function member_tx_sh1(){
		
		$result = true;
		// 启动事务
		Db::startTrans();	
		
		//获取提现信息
		$tixian_info = db('member_tixian')->where("id",input("id"))->find();
		
		if(input("status") == 2){
			//新增用户提现金额
			$res1 = db('member')->where("user_id",$tixian_info["user_id"])->setInc('total_withdrawals_money',$tixian_info['balance']);
			if(!$res1) $result = false;
			
			//更新提现记录
			$res2 = db('member_tixian')->where("id",input("id"))->update(['status' => 2]);
			if(!$res2) $result = false;
			
		}else{
			//退回用户提现金额
			$res3 = db('member')->where("user_id",$tixian_info["user_id"])->setInc('money',$tixian_info['balance']);
			if(!$res3) $result = false;
			//余额变化日志
			$res4 = add_member_charge($tixian_info["user_id"],$tixian_info['balance'],3,0,"提现失败，提现金额回退");
			if(!$res4) $result = false;
			
			
			//更新提现记录
			$res5 = db('member_tixian')->where("id",input("id"))->update(['status' => 3]);
			if(!$res5) $result = false;
		}
		
		if($result){
			// 提交事务
    		Db::commit();
			$this->success('操作成功',url('finance/member_tx'));
		}else{
			//回滚
		    Db::rollback();
			$this->success('操作失败');
		}
	}
	
	//商家提现
	public function agent_tx($status='',$mobile='',$agent_name=''){
		$where=[];
		if($agent_name){
			$where['b.agent_name']=array('like',"%".$agent_name."%");
		}
		$this->assign('agent_name',$agent_name);
		if($mobile){
			$where['b.mobile']=array('like',"%".$mobile."%");
		}
		$this->assign('mobile',$mobile);
		if($status){
			$where['a.cash_state']=$status;
		}
		$this->assign('status',$status);
		$field="a.*,b.agent_name";
		$tx=db('agent_tixian')->alias("a")->join("agent b","a.agent_id=b.id")->where($where)->field($field)->order("add_time desc")->paginate(15);
		$this->assign('list',$tx);
		return $this->fetch();
	}
	
	
	//商家申请提现查看审核
	public function agent_tx_sh(){
		$where['a.id']=input("id");
		$field="a.*,b.agent_name";
		$tx=db('agent_tixian')->alias("a")->join("agent b","a.agent_id=b.id")->where($where)->field($field)->find();
		$this->assign('tx',$tx);
		return $this->fetch();
	}
	
	function agent_tx_sh1(){
		$result = true;
		// 启动事务
		Db::startTrans();	
		
		//获取提现信息
		$tixian_info = db('agent_tixian')->where("id",input("id"))->find();
		
		if(input("status") == 2){
			//新增商家提现金额
			$res1 = db('agent')->where("id",$tixian_info["agent_id"])->setInc('total_withdrawals_money',$tixian_info['balance']);
			if(!$res1) $result = false;
			
			//更新提现记录
			$res2 = db('agent_tixian')->where("id",input("id"))->update(['cash_state' => 2]);
			if(!$res2) $result = false;
			
		}else{
			//退回商家提现金额
			$res3 = db('agent')->where("id",$tixian_info["agent_id"])->setInc('balance',$tixian_info['balance']);
			if(!$res3) $result = false;
			//余额变化日志
			$res4 = add_agent_charge($tixian_info["agent_id"],$tixian_info['balance'],6,0,"提现失败，提现金额回退",1);
			if(!$res4) $result = false;
			
			//更新提现记录
			$res5 = db('agent_tixian')->where("id",input("id"))->update(['cash_state' => 3]);
			if(!$res5) $result = false;
		}
		
		if($result){
			// 提交事务
    		Db::commit();
			$this->success('操作成功',url('finance/member_tx'));
		}else{
			//回滚
		    Db::rollback();
			$this->success('操作失败');
		}
	}
	
	
	function tx_not_pass(){
		$user_type = input('user_type');
		
		$this->assign('user_type',$user_type);
		$this->assign('id',input('id'));
		
		
		if(request()->isAjax()){
			$result = true;
			// 启动事务
			Db::startTrans();
			
			$data['not_pass_msg'] = input('not_pass_msg');
			$data['not_pass_img'] = input('not_pass_img');
			$data['status'] = 3;
			
			if($user_type == 1){	//用户提现
				//提现信息
				$tixian_info = db('member_tixian')->where('id',input('id'))->find();
				//退回用户提现金额
				$res3 = db('member')->where("user_id",$tixian_info["user_id"])->setInc('money',$tixian_info['balance']);
				if(!$res3) $result = false;
				//余额变化日志
				$res4 = add_member_charge($tixian_info["user_id"],$tixian_info['balance'],3,0,"提现失败，提现金额回退");
				if(!$res4) $result = false;
				
				
				//更新提现记录
				$res5 = db('member_tixian')->where("id",input("id"))->update($data);
				if(!$res5) $result = false;
				
				$url = url('system/member_tx');
				
			}else if($user_type == 2){	//商家提现
				//提现信息
				$tixian_info = db('agent_tixian')->where('id',input('id'))->find();
				
				//退回商家提现金额
				$res3 = db('agent')->where("id",$tixian_info["agent_id"])->setInc('balance',$tixian_info['balance']);
				if(!$res3) $result = false;
				//余额变化日志
				$res4 = add_agent_charge($tixian_info["agent_id"],$tixian_info['balance'],6,0,"提现失败，提现金额回退",1);
				if(!$res4) $result = false;
				
				//更新提现记录
				$res5 = db('agent_tixian')
							->where("id",input("id"))
							->update(['cash_state' => 3,'not_pass_msg'=>input('not_pass_msg'),'not_pass_img'=>input('not_pass_img')]);
				if(!$res5) $result = false;
				
				$url = url('system/agent_tx');
				
			}else{//代理提现
				//提现信息
				$tixian_info = db('oem_tixian')->where('id',input('id'))->find();
				//退回用户提现金额
				$res3 = db('oem')->where("id",$tixian_info["oem_id"])->setInc('balance',$tixian_info['balance']);
				if(!$res3) $result = false;
				
				//余额变化日志
				$res4 = add_oem_charge($tixian_info["oem_id"],3,0,$tixian_info['balance'],"提现失败，提现金额回退");
				if(!$res4) $result = false;
				
				
				//更新提现记录
				$res5 = db('oem_tixian')->where("id",input("id"))->update($data);
				if(!$res5) $result = false;
				$url = url('system/oem_tx');
			}
			
			if($result){
				// 提交事务
	    		Db::commit();
	    		return json(['code'=>200,'msg'=>'操作成功','url'=>$url]);
				$this->success('操作成功',$url);
			}else{
				//回滚
			    Db::rollback();
			    return json(['code'=>403,'msg'=>'操作失败']);
			}
		}
		
		return $this->fetch();
	}
}
